package com.breeze.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode-cn.com/problems/permutations/
 * 46. 全排列
 * 给定一个 没有重复 数字的序列，返回其所有可能的全排列。
 * // TODO:
 */
public class LeetCode46 {
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        List<Integer> track = new ArrayList<>();
        backtrace(nums, track);
        return res;
    }

    public void backtrace(int[] nums, List<Integer> track) {
        // 结束条件
        if (track.size() == nums.length) {
            res.add(new ArrayList<>(track));
            return;
        }
        for (int num : nums) {
            if (track.contains(num)) {
                continue;
            }
            // 做选择
            track.add(num);
            // 继续查找下个合适的值
            backtrace(nums, track);
            // 该选择已做尝试，取消选择，继续同级的下个尝试
            track.remove(track.size() - 1);
        }
    }


    public static void main(String[] args) {
        LeetCode46 leetCode46 = new LeetCode46();
        int[] nums = {1, 2, 3};
        List<List<Integer>> permute = leetCode46.permute(nums);
        System.out.println(permute);
    }
}
